package com.think.archive.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.think.archive.mapper.SysFuncMapper;
import com.think.archive.model.SysDept;
import com.think.archive.model.SysDeptExample;
import com.think.archive.model.SysFunc;
import com.think.archive.model.SysFuncExample;
import com.think.archive.service.SysFuncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysFuncServiceImpl implements SysFuncService {

    @Autowired
    SysFuncMapper funcMapper;

    @Override
    public List<SysFunc> selectAllFunc(){
        List<SysFunc> permissions=funcMapper.selectByExample(null);
        return permissions;
    }

    @Override
    public List<SysFunc> selectListByCode(String code) {
        SysFuncExample example=new SysFuncExample();
        example.createCriteria().andFuncCodeLike(code+"__");
        example.setOrderByClause("sort asc");
        List<SysFunc> permissions=funcMapper.selectByExample(example);
        return permissions;
    }

    @Override
    public int insertOrUpdateFunc(SysFunc permission) {
        SysFunc permissionSave=funcMapper.selectByPrimaryKey(permission.getId());
        if(permissionSave==null){
            permission.setCreater("system");
            permission.setCreateTime(new Date());
            return funcMapper.insert(permission);
        }
        else{
            permissionSave.setFuncName(permission.getFuncName());
            permissionSave.setFuncCode(permission.getFuncCode());
            permissionSave.setPath(permission.getPath());
            permissionSave.setRemark(permission.getRemark());
            return  funcMapper.updateByPrimaryKey(permissionSave);
        }
    }

    @Override
    public int deleteFunc(int id) {
        return funcMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int changeStatusFunc(int id, String status) {
        SysFunc permission=funcMapper.selectByPrimaryKey(id);
        permission.setStatus(status);
        return funcMapper.updateByPrimaryKey(permission);
    }

    @Override
    public SysFunc selectByFuncCode(String funcCode) {
        SysFuncExample example=new SysFuncExample();
        example.createCriteria().andFuncCodeEqualTo(funcCode);
        List<SysFunc> funcs=funcMapper.selectByExample(example);
        if(funcs.isEmpty()){
            return null;
        }else{
            return funcs.get(0);
        }
    }

    @Override
    public JSONArray findFuncTree(String funcCode) {
        JSONArray jsonArray=findNextTree("",funcCode);
        return jsonArray;
    }

    @Override
    public List<SysFunc> findNextLevelFunc(String funcCode) {
        SysFuncExample example=new SysFuncExample();
        SysFuncExample.Criteria criteria=example.createCriteria();
        criteria.andFuncCodeLike(funcCode+"__");
        List<SysFunc> funcs=funcMapper.selectByExample(example);
        return funcs;
    }

    public JSONArray findNextTree(String nodeCode,String funcCode){
        JSONArray leaf=new JSONArray();
        List<SysFunc> funcs=findNextLevelFunc(nodeCode);
        for(SysFunc func:funcs){
            JSONObject node=new JSONObject();
            node.put("text",func.getFuncName());
            node.put("code",func.getFuncCode());
            node.put("href","/admin/func/list?funcCode="+func.getFuncCode());
            JSONObject state=new JSONObject();
            JSONArray jsonArray=findNextTree(func.getFuncCode(),funcCode);
            if(funcCode.startsWith(func.getFuncCode())){
                state.put("expanded",true);
            }else{
                state.put("expanded",false);
            }
            if(funcCode.equals(func.getFuncCode())){
                state.put("selected",true);
            }else{
                state.put("selected",false);
            }
            node.put("state",state);
            if(jsonArray.size()>0){
                node.put("nodes",jsonArray);
            }
            leaf.add(node);
        }
        return leaf;
    }
}
